<template>
  <div class="app-container">
    <!-- 筛选 -->
    <div class="select-box">
      <div class="search-icon">
        <i class="el-icon-search"></i>
        <span>  筛选搜查</span>
        <div class="search-button">
          <el-button  @click="resetQuery">重置</el-button>
          <el-button type="primary"  @click="handleQuery">查询搜索</el-button>
        </div>
      </div>
      <el-form  :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
        <el-form-item class="inline" label="公告标题:  " prop="noticeTitle" >
          <el-input
            v-model="queryParams.noticeTitle"
            placeholder="筛选公告标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item class="inline" label="发布时间:  " prop="dateRange">
          <el-date-picker
            v-model="queryParams.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            placeholder="选择时间范围"
            value-format="yyyy-MM-dd"
            @keyup.enter.native="handleQuery"
            unlink-panels  >
          </el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    

    <!-- 标题 -->
    <el-row>
      <div class="list-title">
        <div class="title">
          <span class="el-icon-tickets"></span>
          <span class="title-font">公告列表</span>
        </div>
        <div class="btn-add">
          <el-button size="small" type="primary" plain @click="handleAdd()">添加</el-button>
        </div>
      </div>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="noticeList" border>
      <el-table-column label="ID" align="center" prop="noticeId" width="100" />
      <el-table-column
        label="公告标题"
        align="center"
        prop="noticeTitle"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="170">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      :current-page="pagination.current"
      :page-size="pagination.pageSize"
      layout="total, prev, pager, next"
      :total="pagination.total"
      @pagination="getList"
      @current-change="onPageChange"
      class="paper"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getNoticeList, deleteNotice } from '@/api/announce/list';

export default {
  name: "Notice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 公告表格数据
      noticeList: [],
      // 查询参数
      queryParams: {
        noticeTitle: undefined,
        dateRange:[]
      },
      // 分页参数
      pagination:{
        total:0,
        current:1,
        pageSize:10,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      getNoticeList({
        pageIndex: this.pagination.current,
        pageSize: this.pagination.pageSize,
        queryParams:this.queryParams
      }).then( res =>{
        // console.log(res)
        let data = res.data.data;
        this.noticeList = data.noticeList;
        this.pagination.total = data.total;
        this.pagination.current = data.pageIndex;
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pagination.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({name:"announcementAdd"});
    },
    // 查看详情
    handleDetail(row) {
      this.$router.push({name:"announcementDetail",params:{row}});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId;
      this.$modal.confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？').then(function() {
        return deleteNotice({id:noticeIds});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 页号发生变化
    onPageChange(number){
      this.pagination.current=number;
      this.getList();
    }
  }
};
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 20px;
}
.list-title {
  height: 50px;
  width: 100%;
  border: solid 1px #DCDFE6;

  .title {
    margin: 14px;
    float: left;
  }

  .title-font {
    margin-left: 5px;
  }
}

.btn-add {
  margin: 10px;
  float: right;
}
.select-box{
  border: 1px solid rgb(0, 0, 0,0.1);
  margin-bottom:20px;
  padding: 20px;
}
.inline{
  margin-right: 30px;
}
.search-icon{
  padding-bottom: 20px
}
.search-button{
  float: right;
}
.paper{
  float: right;
  margin-top: 10px;
}
</style>
